package io.mft.api.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import io.mft.commons.function.response.kv.KVMap;
import io.mft.commons.util.Unique;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@JsonInclude(Include.NON_DEFAULT)
public class Input implements Unique,ActualKeyInitor,Serializable{
	
	 
	        /**
	 * @说明：TODO
	 * @字段名称 serialVersionUID
	        */  
	    
	private static final long serialVersionUID = -5074724158015175380L;
	public static enum InputType{
		simple,
		//jsonFormat,
		singlejsonFormat
	}
	
	//private InputType inputType=InputType.simple;
	private List<InputParameter> inputParameters;
	@JsonIgnore
	private final Map<String, InputParameter> map = new HashMap<>();
	@JsonIgnore
	private final Map<String, InputParameter> actualMap = new HashMap<>();
	@JsonIgnore
	private final KVMap actualKeyMap = new KVMap();
	boolean toUnderlineCase=false;
	@JsonIgnore
	private boolean containPrimaryKey=false;
	@JsonIgnore
	private InputParameter primaryKey;
	//private InputParameter singlejsonFormatInputParameter;
	//private Api api;
	
	@Override
	public String uniqueId() {
		String k="";
		for (InputParameter inputParameter : inputParameters) {
			k=k+"-"+inputParameter.uniqueId();
		}
		k=k+"-"+toUnderlineCase;
		return k;
	}
	
	
	public Map<String, InputParameter> inputParameterMap() {
		return map;
	}
	
	public Map<String, InputParameter> actualInputParameterMap() {
		return actualMap;
	}
	
	public void init() {
	//	this.api=api;
		KVMap map=initActualKey(toUnderlineCase);
		actualKeyMap.putAll(map);
		initInputParameter();
	}
	public String actualKey(String key) {
		return actualKeyMap.getStr(key);
	}
	private void initInputParameter() {
		inputParameters.forEach(in -> {
			map.put(in.getKey(), in);
			actualMap.put(in.getActualKey(), in);
			if (in.isPrimaryKey()) {
				if (!containPrimaryKey) {
					primaryKey=in;
					containPrimaryKey=true;
				}
			}
			/**
			if (inputType==InputType.singlejsonFormat) {
				if (ObjectUtil.isNotNull(singlejsonFormatInputParameter)) {
					throw new MFTException("接口["+api.getKey()+"]输入类型singlejsonFormat不允许配置多个InputParameterDataType.json或者InputParameterDataType.jsonArray");
				}
				if (in.getDataType()==InputParameterDataType.json||in.getDataType()==InputParameterDataType.jsonArray) {
					singlejsonFormatInputParameter=in;
				}
			}
			**/
		});
	}
	@Override
	public KVMap initActualKey(boolean toUnderlineCase) {
		KVMap map=new KVMap();
		for (InputParameter inputParameter : inputParameters) {
			KVMap m=inputParameter.initActualKey(toUnderlineCase);
			map.putAll(m);
		}
		return map;
	}
	
	public boolean containPrimaryKey() {
		return containPrimaryKey;
	}
}
